﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.TextEditor.Document;
using Openlab.Tools.CodeGender.StoredProcedures;
using Openlab.Tools.CodeGender.Funtions;
using Openlab.Tools.CodeGender.Classes;

namespace Openlab.Tools.CodeGender
{
    public partial class frmMain : Form
    {
        string currentTable = null;
        public frmMain()
        {
            InitializeComponent();
            LoadData();
        }

        private void lvColumns_Resize(object sender, EventArgs e)
        {
            chRemarks.Width = lvColumns.Width - chName.Width - chIsPrimaryKey.Width - chTypeName.Width - 5;
        }

        private void btnGenerateEntity_Click(object sender, EventArgs e)
        {
            List<Column> columns = CodeOption.Columns;
            Column idColumn = CodeOption.IdColumn;
            if (!string.IsNullOrEmpty(currentTable) && columns.Count > 0)
            {
                Class c = new TinyEntityClass(CodeOption);
                editorEntity.Text = c.Code;
                GenerateSqlCode(CodeOption);
                GenerateDataAccessCode(CodeOption);
                GenerateManagerCode(idColumn);
            }
        }

        void GenerateManagerCode(Column idColumn)
        {
            StringBuilder code = new StringBuilder();

            Class manager = new ManagerClass(CodeOption);
            code.AppendLine(manager.Code);

            editorManager.Text = code.ToString();
        }

        void GenerateDataAccessCode(CodeOption option)
        {
            StringBuilder code = new StringBuilder();

            Function funCreateEntity = new CreateEntityDAFunc(option.ClassName, option.CreateEntityStoredProcedureName, option.Columns, option.IdColumn);
            code.Append(funCreateEntity.Code);
            code.AppendLine();

            Function funUpdateEntity = new UpdateEntityDAFunc(option.ClassName, option.UpdateEntityStoredProcedureName, option.Columns, option.IdColumn);
            code.Append(funUpdateEntity.Code);
            code.AppendLine();

            Function funDeleteEntity = new DeleteEntityDAFunc(option.ClassName, option.DeleteEntityStoredProcedureName, option.IdColumn);
            code.Append(funDeleteEntity.Code);
            code.AppendLine();

            Function fun1 = new GetEntityIdsDAFunc(option.ClassName, txtGetIdsStoredProcedureName.Text, option.IdColumn);
            code.Append(fun1.Code);
            code.AppendLine();

            Function fun2 = new GetEntitiesDAFunc(option.ClassName, txtGetEntitiesStoredProcedureName.Text, option.IdColumn);
            code.Append(fun2.Code);
            code.AppendLine();

            editorDataAccess.Text = code.ToString();
        }

        void GenerateSqlCode(CodeOption option)
        {
            Table table = new Table(currentTable);
            StringBuilder sql = new StringBuilder();

            StoredProcedure sqlCreateEntitiy = new CreateEntitySP(option.CreateEntityStoredProcedureName, option.Author, option.Columns, option.IdColumn, table);
            sql.AppendLine(sqlCreateEntitiy.Sql);
            sql.AppendLine();

            StoredProcedure sqlUpdateEntitiy = new UpdateEntitySP(option.UpdateEntityStoredProcedureName, option.Author, option.Columns, option.IdColumn, table);
            sql.AppendLine(sqlUpdateEntitiy.Sql);
            sql.AppendLine();

            StoredProcedure sqlDeleteEntitiy = new DeleteEntitySP(option.DeleteEntityStoredProcedureName, option.Author, option.IdColumn, table);
            sql.AppendLine(sqlDeleteEntitiy.Sql);
            sql.AppendLine();

            StoredProcedure sqlGetIds = new GetIdsSP(option.GetEntityIdsStoredProcedureName, option.Author, option.IdColumn, table);
            sql.AppendLine(sqlGetIds.Sql);
            sql.AppendLine();

            StoredProcedure sqlGetEntities = new GetEntitiesSP(option.GetEntitiesStoredProcedureName, option.Author, option.Columns, option.IdColumn, table);
            sql.AppendLine(sqlGetEntities.Sql);
            sql.AppendLine();

            editorStoredProcedures.Text = sql.ToString();
        }


        CodeOption CodeOption
        {
            get
            {
                Column idColumn;
                List<Column> columns = GetSelectedColumns(out idColumn);
                CodeOption option = new CodeOption();
                option.Author = txtAuthor.Text;
                option.ClassName = txtClassName.Text;
                option.NamespaceName = txtNameSpace.Text;
                option.ManagerClassName = txtManagerClassName.Text;
                option.GetEntitiesStoredProcedureName = txtGetEntitiesStoredProcedureName.Text;
                option.GetEntityIdsStoredProcedureName = txtGetIdsStoredProcedureName.Text;
                option.CreateEntityStoredProcedureName = txtCreateEntityStoredProcedureName.Text;
                option.UpdateEntityStoredProcedureName = txtUpdateEntityStoredProcedureName.Text;
                option.DeleteEntityStoredProcedureName = txtDeleteEntityStoredProcedureName.Text;
                option.IdColumn = idColumn;
                option.Columns = columns;
                return option;
            }
        }
    }
}
